package com.exam.controller;

import com.exam.serviceimpl.BigModelNew;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;


@CrossOrigin(origins = "http://localhost:8088")


@RestController
@RequestMapping("/api")
public class QuestionController {
    private BigModelNew bigModelNew;
    public static String Answer;
    @PostMapping("/ask")
    public Response askQuestion(@RequestBody Request request) throws Exception {
        String question = request.getQuestion();
        if (question == null || question.isEmpty()) {
            return new Response(400, "问题不能为空", null);
        }
        // 将问题传递给大模型处理
        try {
            //bigModelNew = new BigModelNew("userId", false);
            BigModelNew.flag=false;

            //BigModelNew.NewQuestion = question; // 设置问题
            // 这里应该调用 bigModelNew 的方法来处理问题，并获取答案
            // 例如：bigModelNew.processQuestion();
            // 模拟获取答案
            BigModelNew.sendQuestion(question);

            while (true)
            {
                if(BigModelNew.flag==true)
                {
                    break;

                }

            }
            String answer = BigModelNew.totalAnswer; // 假设这是从大模型获取的答案
            return new Response(200, "问题已回答", answer);

        } catch (Exception e) {
            // 异常处理
            return new Response(500, "服务器内部错误", null);
        }






    }

    public static class Request {
        private String question;

        // Getter and Setter
        public String getQuestion() {
            return question;
        }

        public void setQuestion(String question) {
            this.question = question;
        }
    }

    public static class Response {
        private int code;
        private String message;
        private String answer;

        // Constructor
        public Response(int code, String message, String answer) {
            this.code = code;
            this.message = message;
            this.answer = answer;
        }

        // Getters and Setters
        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getAnswer() {
            return answer;
        }

        public void setAnswer(String answer) {
            this.answer = answer;
        }
    }
}
